package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.CustomException;
import com.itheima.domian.*;
import com.itheima.domian.vo.OrderVo;
import com.itheima.domian.vo.QueryVo;
import com.itheima.mapper.client.OrderMapper;
import com.itheima.service.client.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;


/**
 * @Author 12441
 * @Date 2022/8/11 15:13
 * @Version 1.0
 */
@Service
@Slf4j
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Orders> implements OrderService {

    @Autowired
    private UserService userService;
    @Autowired
    private AddressBookService addressBookService;
    @Autowired
    private ShoppingCartService shoppingCartService;
    @Autowired
    private OrderDetailsService orderDetailsService;
    @Autowired
    private OrderMapper orderMapper;

    @Override
    public void submitOrder(Orders order, String currentId) {
        //查询用户数据 -地址 -购物车 -用户
        //1.用户
        User user = userService.getById(currentId);

        //2.地址
        String addressBookId = order.getAddressBookId();
        //生成一个订单号，赋值给订单
        long randomId = IdWorker.getId();


        AddressBook addressBook = addressBookService.getById(addressBookId);
        if (addressBook==null){
           throw new CustomException("用户地址有误");
        }
        log.info(">>>>>>>>>"+addressBookId);

        //3.购物车
        //具有原子操作的Integer数据类型
        AtomicInteger amount = new AtomicInteger(0);
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,currentId);
        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);
        if(list==null){
            throw new CustomException("购物车为空，不可下单");
        }
        List<OrderDetails> collect = list.stream().map((item) -> {
            OrderDetails orderDetails = new OrderDetails();
            orderDetails.setOrderId(randomId);
            orderDetails.setNumber(item.getNumber());
            orderDetails.setDishFlavor(item.getDishFlavor());
            orderDetails.setDishId(item.getDishId());
            orderDetails.setSetmealId(item.getSetmealId());
            orderDetails.setName(item.getName());
            orderDetails.setAmount(item.getAmount());

            amount.addAndGet((int) (item.getAmount()*(new BigDecimal(item.getNumber())).intValue()));

            return orderDetails;
        }).collect(Collectors.toList());


        //插入数据到订单
        order.setNumber(String.valueOf(randomId));
        order.setOrderTime(LocalDateTime.now());
        order.setCheckoutTime(LocalDateTime.now());
        order.setStatus(2);
        order.setAmount(String.valueOf(new BigDecimal(amount.get())));
        order.setUserId(currentId);
        order.setUserName(user.getName());
        order.setConsignee(addressBook.getConsignee());
        order.setPhone(addressBook.getPhone());
        order.setAddress(addressBook.getDetail());

        this.save(order);
        //插入多条订单明细
        orderDetailsService.saveBatch(collect);

        shoppingCartService.remove(queryWrapper);


    }

    @Override
    public IPage<OrderVo> findByPage(Page<OrderVo> page, QueryWrapper<OrderVo> queryWrapper) {
        return orderMapper.findByPage(page, queryWrapper);
    }
}
